﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace LdCms.Plugins.Sms.Tencent
{
    using LdCms.Common.Extension;
    using LdCms.Common.Utility;
    using LdCms.Common.Security;
    using LdCms.Common.Web;
    using LdCms.Common.Logs;
    using LdCms.Common.Net;
    using LdCms.Common.Json;
    using LdCms.Common.Time;

    public class TencentSmsHelper
    {
        public string AppID { get; set; }
        public string AppKey { get; set; }
        public TencentSmsHelper() { }
        public TencentSmsHelper(string appId, string appKey)
        {
            AppID = appId;
            AppKey = appKey;
        }

        private string strRand = GeneralCodeHelper.GetRandomInt(8);
        private string sendSmsUrl = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms";
        private string sendMultiSmsUrl = "https://yun.tim.qq.com/v5/tlssmssvr/sendmultisms2";
        private string pullStatusMobileUrl = "https://yun.tim.qq.com/v5/tlssmssvr/pullstatus4mobile";

        private string pullSendStatusUrl = "https://yun.tim.qq.com/v5/tlssmssvr/pullsendstatus";
        private string pullCallbackStatusUrl = "https://yun.tim.qq.com/v5/tlssmssvr/pullcallbackstatus";
        private string getSmsPackagesUrl = "https://yun.tim.qq.com/v5/tlssmssvr/getsmspackages";

        private string CreateSendSmsUrl()
        {
            var sParams = new
            {
                sdkappid = AppID,
                random = strRand
            };
            string queryString = Utility.ObjToUrlParams(sParams);
            return string.Format("{0}?{1}", sendSmsUrl, queryString);
        }
        private string CreateSendMultiSmsUrl()
        {
            var sParams = new
            {
                sdkappid = AppID,
                random = strRand
            };
            string queryString = Utility.ObjToUrlParams(sParams);
            return string.Format("{0}?{1}", sendMultiSmsUrl, queryString);
        }
        private string CreatePullStatusMobileUrl()
        {
            var sParams = new
            {
                sdkappid = AppID,
                random = strRand
            };
            string queryString = Utility.ObjToUrlParams(sParams);
            return string.Format("{0}?{1}", pullStatusMobileUrl, queryString);
        }

        private string CreatePullSendStatusUrl()
        {
            var sParams = new
            {
                sdkappid = AppID,
                random = strRand
            };
            string queryString = Utility.ObjToUrlParams(sParams);
            return string.Format("{0}?{1}", pullSendStatusUrl, queryString);
        }
        private string CreatePullCallbackStatusUrl()
        {
            var sParams = new
            {
                sdkappid = AppID,
                random = strRand
            };
            string queryString = Utility.ObjToUrlParams(sParams);
            return string.Format("{0}?{1}", pullCallbackStatusUrl, queryString);
        }
        private string CreateGetSmsPackagesUrl()
        {
            var sParams = new
            {
                sdkappid = AppID,
                random = strRand
            };
            string queryString = Utility.ObjToUrlParams(sParams);
            return string.Format("{0}?{1}", getSmsPackagesUrl, queryString);
        }

        private string PostUrl(string url, string data)
        {
            try
            {
                string result = HttpHelper.PostToUrl(url, data);
                LogsManager.LogPath = string.Empty;
                LogsManager.logFielPrefix = string.Empty;
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-腾讯短信--开结--", strRand));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-IP：{1}", strRand, Net.Ip));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-地址：{1}", strRand, url));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-参数：{1}", strRand, data));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-结果：{1}", strRand, result.MiniClear()));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-腾讯短信--结束--", strRand));
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string SendSms(string data)
        {
            try
            {
                string url = CreateSendSmsUrl();
                return PostUrl(url, data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string SendSms(SendSms entity)
        {
            try
            {
                int timestamp = entity.time;
                if (entity.time.ToInt() == 0)
                    timestamp = TimeHelper.GetUnixTimestampTen();
                long random = strRand.ToLong();
                long time = timestamp;
                string mobile = entity.tel.mobile;
                entity.sig= Utilities.GetSignature(AppKey, random, time, mobile);
                entity.time = time.ToInt();
                string data = entity.ToJson();
                return SendSms(data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public SendSmsResult SendSmsPro(SendSms entity)
        {
            try
            {
                string result = SendSms(entity);
                return result.ToObject<SendSmsResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string SendMultiSms(string data)
        {
            try
            {
                string url = CreateSendMultiSmsUrl();
                return PostUrl(url, data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string SendMultiSms(SendMultiSms entity)
        {
            try
            {
                long random = strRand.ToLong();
                long time = entity.time;
                string[] mobile = entity.tel.Select(x => x.mobile).ToArray();
                entity.sig = Utilities.GetSignature(AppKey, random, time, mobile);
                string data = entity.ToJson();
                return SendMultiSms(data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public SendMultiSmsResult SendMultiSmsPro(SendMultiSms entity)
        {
            try
            {
                string result = SendMultiSms(entity);
                return result.ToObject<SendMultiSmsResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string PullStatusMobile(string data)
        {
            try
            {
                string url = CreatePullStatusMobileUrl();
                return PostUrl(url, data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string PullStatusMobile(PullStatusMobile entity)
        {
            try
            {
                long random = strRand.ToLong();
                long time = entity.time;
                entity.sig = Utilities.GetSignature(AppKey, random, time);
                string data = entity.ToJson();
                return PullStatusMobile(data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public PullStatusMobileResult PullStatusMobilePro(PullStatusMobile entity)
        {
            try
            {
                string result = PullStatusMobile(entity);
                return result.ToObject<PullStatusMobileResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #region 统计部分
        public string PullSendStatus(string data)
        {
            try
            {
                string url = CreatePullSendStatusUrl();
                return PostUrl(url, data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string PullSendStatus(PullSendStatus entity)
        {
            try
            {
                long random = strRand.ToLong();
                long time = entity.time;
                entity.sig = Utilities.GetSignature(AppKey, random, time);
                string data = entity.ToJson();
                return PullSendStatus(data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public PullSendStatusResult PullSendStatusPro(PullSendStatus entity)
        {
            try
            {
                string result = PullSendStatus(entity);
                return result.ToObject<PullSendStatusResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string PullCallbackStatus(string data)
        {
            try
            {
                string url = CreatePullCallbackStatusUrl();
                return PostUrl(url, data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string PullCallbackStatus(PullCallbackStatus entity)
        {
            try
            {
                long random = strRand.ToLong();
                long time = entity.time;
                entity.sig = Utilities.GetSignature(AppKey, random, time);
                string data = entity.ToJson();
                return PullCallbackStatus(data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public PullCallbackStatusResult PullCallbackStatusPro(PullCallbackStatus entity)
        {
            try
            {
                string result = PullCallbackStatus(entity);
                return result.ToObject<PullCallbackStatusResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string GetSmsPackages(string data)
        {
            try
            {
                string url = CreateGetSmsPackagesUrl();
                return PostUrl(url, data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetSmsPackages(GetSmsPackages entity)
        {
            try
            {
                long random = strRand.ToLong();
                long time = entity.time;
                entity.sig = Utilities.GetSignature(AppKey, random, time);
                string data = entity.ToJson();
                return GetSmsPackages(data);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public GetSmsPackagesResult GetSmsPackagesPro(GetSmsPackages entity)
        {
            try
            {
                string result = GetSmsPackages(entity);
                return result.ToObject<GetSmsPackagesResult>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

    }
}
